<?php
//刷还大额综合通道
namespace app\pay\service;
class Hqdeg extends Hqbase
{
	protected $mchId = "jn2019032514062";//商户号
	protected $signKey = "baede106";//密钥
	protected $version = "0100";
	protected $apiUrl = "http://pay.huanqiuhuiju.com/authsys/api/large/channel/pay/execute.do";

	/**
	 * 鉴权
	 *
	 * @param array $param
	 *
	 * @return bool|mixed
	 */
	public function register($param = [])
	{
		if (empty($param) || !is_array($param)) {
			return false;
		}

		$data = [
			//公共参数
			"methodname" => "register",//方法名
			"transcode" => "051",//交易码
			"version" => $this->version,//版本号
			"merchno" => $this->mchId,//商户号

			//业务参数
			"ordersn" => $param["ordersn"],//流水号
			"dsorderid" => $param["dsorderid"],//商户订单号
			"bankcard" => $param["bankcard"],//银行卡号
			"username" => $param["username"],//真实姓名
			"idcard" => $param["idcard"],//身份证号
			"mobile" => $param["mobile"],//预留手机号
			"futureRateValue" => (string)$param["futureRateValue"],//扣款费率如：0.5 表示0.5%
			"fixAmount" => (string)$param["fixAmount"],//还款手续费以分为单位，不能有小数点
		];
		$res = $this->unified($data);
		return $res;
	}


	/**
	 * 修改费率
	 * @return bool|mixed|string
	 */
	public function updateRage($param)
	{
		if (empty($param) || !is_array($param)) {
			return false;
		}

		$data = [
			"methodname" => "update",//方法名
			"transcode" => "051",//交易码;037落地还款H;902查询接口
			"version" => $this->version,//版本号
			"merchno" => $this->mchId,//商户号

			"ordersn" => $param["ordersn"],//流水号
			"dsorderid" => $param["dsorderid"],//商户订单号
			"subMerchantNo" => $param["subMerchantNo"],//子商户号
			"futureRateValue" => $param["bankcard"],//扣款费率
			"fixAmount" => $param["username"],//还款手续费
		];
		$res = $this->unified($data);
		return $res;
	}


	//客户绑卡
	public function bindCard($param)
	{
		if (empty($param) || !is_array($param)) {
			return false;
		}


		$data = [
			"methodname" => "bindCard",//方法名
			"transcode" => "051",//交易码;015落地还款H;902查询接口
			"version" => $this->version,//版本号
			"ordersn" => $param["ordersn"],//流水号
			"dsorderid" => $param["dsorderid"],//商户订单号
			"merchno" => $this->mchId,//商户号
			"subMerchantNo" => $param["subMerchantNo"],//子商户号

			"bankcard" => $param["bankcard"],//银行卡
			"username" => $param["username"],//姓名
			"idcard" => $param["idcard"],//身份证
			"mobile" => $param["mobile"],//手机号
			"cardType" => $param["cardType"],//卡类型01.借记卡 02.贷记卡

			"returnUrl" => !empty($param["returnUrl"]) ? $param["returnUrl"] : "",//前台跳转地址
			"notifyUrl" => !empty($param["notifyUrl"]) ? $param["notifyUrl"] : "",//异步通知地址
		];
		if ($param["cardType"] == "02") {
			$data["cvn2"] = $param["cvn2"];//CVN2
			$data["expireDate"] = $param["expireDate"];//信用卡有效期
		}
		$res = $this->unified($data);
		return $res;
	}


	//扣款
	public function pay($param)
	{
		if (empty($param) || !is_array($param)) {
			return false;
		}


		$data = [
			"methodname" => "pay",//方法名
			"transcode" => "051",//交易码;015落地还款H;902查询接口
			"version" => "0100",//版本号
			"ordersn" => $param["ordersn"],//流水号
			"dsorderid" => $param["dsorderid"],//商户订单号
			"merchno" => $this->mchId,//商户号
			"subMerchantNo" => $param["subMerchantNo"],//子商户号


			"bindId" => $param["bindId"],//绑卡ID
			"bankcard" => $param["bankcard"],//银行卡号
			"amount" => (string)$param["amount"],//金额
			//"userFee"=>$param["userFee"],//交易手续费
			//"province"=>$param["province"],//地区代码xuan
			//"mcc"=>$param["mobile"],//行业xuan
			"notifyUrl" => $param["notifyUrl"],//异步通知地址
		];
		if ($param["userFee"] >= 0 && isset($param["userFee"])) {//交易手续费xuan
			$data["userFee"] = (string)$param['userFee'];
		}
		if (!empty($param["province"]) && isset($param["province"])) {//消费城市编码
			$data["province"] = $param['province'];
		}
		if (!empty($param["mcc"]) && isset($param["mcc"])) {//mcc行业类型码
			if (is_numeric($param["mcc"])) {
				$data["mcc"] = $param['mcc'];
			}
		}
		//halt($data);
		$res = $this->unified($data);
		return $res;
	}


	//代付（还款）
	public function withDraw($param)
	{
		if (empty($param) || !is_array($param)) {
			return false;
		}

		$data = [
			"methodname" => "withDraw",//方法名
			"transcode" => "051",//交易码;015落地还款H;902查询接口
			"version" => $this->version,//版本号
			"ordersn" => $param["ordersn"],//流水号
			"dsorderid" => $param["dsorderid"],//商户订单号
			"merchno" => $this->mchId,//商户号
			"subMerchantNo" => $param["subMerchantNo"],//子商户号

			"cardType" => $param["cardType"],//卡类型01.借记卡 02.贷记卡
			"bankcard" => $param["bankcard"],//银行卡号
			"amount" => (string)$param["amount"],//金额
			"userFee" => (string)$param["userFee"],//交易手续费
			"bindId" => $param["bindId"],//绑卡ID
			"notifyUrl" => $param["notifyUrl"],//异步通知地址
		];
		$res = $this->unified($data);
		return $res;
	}


	//商户余额接口
	public function queryBalance($param)
	{
		if (empty($param) || !is_array($param)) {
			return false;
		}


		$data = [
			"methodname" => "queryBalance",//方法名
			"transcode" => "051",//交易码;015落地还款H;902查询接口
			"version" => $this->version,//版本号
			"merchno" => $this->mchId,//商户号
			"subMerchantNo" => $param["subMerchantNo"],//子商户号

			"ordersn" => $param["ordersn"],//流水号
			"dsorderid" => $param["dsorderid"],//商户订单号
			"bindId" => $param["bindId"],//绑卡Id
		];
		$res = $this->unified($data);
		return $res;
	}


	//订单查询接口
	public function queryOrder($param)
	{
		if (empty($param) || !is_array($param)) {
			return false;
		}


		$data = [
			"transcode" => "902",//订单查询
			"version" => $this->version,//版本号

			"ordersn" => $param["ordersn"],//流水号
			"dsorderid" => $param["dsorderid"],//商户订单号
			"merchno" => $this->mchId,//商户号
			"transtype" => $param["transtype"],//交易类型95交易96代付
		];
		//halt($data);
		$res = $this->unified($data, "http://pay.huanqiuhuiju.com/authsys/api/auth/execute.do");
		return $res;
	}
}